#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>

using namespace std;

const int maxn = 333, inf = 0x7fffffff;
int a[maxn], dis[maxn][maxn], dp[maxn][maxn], s[maxn][maxn];

void init(int m, int n)
{
	for (int i = 0; i <= m + 1; i++)
	{
		for (int j = 0; j <= n + 1; j++)
		{
			dp[i][j] = 0;
			dis[i][j] = 0;
		}
	}
}

void read(int n)
{
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for (int i = 1; i <= n; i++)
		for (int j = i; j <= n; j++)
			dis[i][j] = dis[i][j - 1] + a[j] - a[(i + j) / 2];
	for (int i = 1; i <= n; i++)
		dp[1][i] = dis[1][i];
}

int main()
{
	int n, m;
	while (scanf("%d %d", &n, &m) != EOF)
	{
		init(m, n);
		read(n);
		for (int i = 2; i <= m; i++)
		{
			s[i][n + 1] = n - 1;
			for (int j = n; j >= i; j--)
			{
				if (i == j)
					dp[i][j] = 0;
				else
					dp[i][j] = inf;
				for (int k = s[i - 1][j]; k <= s[i][j + 1]; k++)
				{
					if (dp[i][j] > dp[i - 1][k] + dis[k + 1][j])
					{
						dp[i][j] = dp[i - 1][k] + dis[k + 1][j];
						s[i][j] = k;
					}
				}
			}
		}
		printf("%d\n", dp[m][n]);
	}
	return 0;
}